package com.gmall.product.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.gmall.common.result.Result;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/admin/product/test")
@RefreshScope// 支持Nacos的动态刷新功能
public class TestController{

    @Value("${yjs.name}")
    String name;
    @GetMapping("test1")
    @ResponseBody
    public String test1(){
        return name;
    }

    @Value("${server.port}")
    String port;
    @GetMapping("testPort")
    @ResponseBody
    public String testPort(){
        return port;
    }

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedissonClient redissonClient;
    int num = 0;
    @GetMapping("testLock")
    @ResponseBody
    public synchronized Result testLock() {
        RLock myLock = redissonClient.getLock("myLock");
        // 查询redis中的num值
        myLock.lock();//Hash myLock  UUID+ThreadID   1
        System.out.println("ThreadID:" + Thread.currentThread().getId());
        try {
            String value = (String) this.redisTemplate.opsForValue().get("num");
            // 没有该值return
            if (value != null && !value.equals("")) {
                num = Integer.parseInt(value);
            }
            // 把redis中的num值+1
            this.redisTemplate.opsForValue().set("num", String.valueOf(++num));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            myLock.unlock();//如果当前线程ID = 小Key的ThreadID，并且UUID相同，才允许删除
        }
        return Result.ok();
    }


    @GetMapping("/captcha")
    public void generateCaptcha(HttpServletResponse response) throws Exception {
        // 创建一个线型验证码实例
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 40, 4,40); // 宽度、高度、字符和干扰线数量可自定义

        // 设置验证码内容和输出到http响应
        String code = captcha.getCode(); // 获取验证码文本内容
        System.out.println("验证码内容是："+ code);
        captcha.write(response.getOutputStream()); // 输出图片到浏览器
        response.flushBuffer();
    }

    @GetMapping("/captcha1")
    @ResponseBody
    public String generateCaptchaAsync() throws Exception {
        // 创建一个线型验证码实例
        //https://www.jianshu.com/p/3238bb3bcad8
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 40, 4,40); // 宽度、高度、字符和干扰线数量可自定义

        // 设置验证码内容和输出到http响应
        String code = captcha.getCode(); // 获取验证码文本内容
        System.out.println("验证码内容是："+ code);
        String imageBase64 = captcha.getImageBase64();
        String imageBase64Data = captcha.getImageBase64Data();

        System.out.println("不带数据格式前缀:"+imageBase64);
        System.out.println("带数据格式前缀:"+imageBase64Data);
        //"data:image/png;base64,"
        return imageBase64;
    }
}
